package com.spit.fammo.ui.frg

import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.spit.amoeba.base.BaseFragment
import com.spit.fammo.R
import com.spit.fammo.adapter.AssetDetailsAdapter
import com.spit.fammo.databinding.FAssetTextBinding
import com.spit.fammo.ext.init
import com.spit.fammo.mar.eventViewModel
import com.spit.fammo.util.recyclerview.RecycleViewDivider
import com.spit.fammo.viewmodel.AssetModel
import org.json.JSONArray


/**
 * @Author nike
 * @Date 2023/8/2 14:18
 * @Description rfid 展示详情
 */
class AssetTextFrg: BaseFragment<AssetModel, FAssetTextBinding>() {

    var jsonArray = JSONArray()

    val adapter by lazy { activity?.let { AssetDetailsAdapter(it, jsonArray) } }

    var bean: String? = null

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            bean = it.getString("bean")
        }
        mDatabind.swipeRefresh.isEnabled = false
        //初始化recyclerView
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter!!).let {
//            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(0f), ConvertUtils.dp2px(0f), true))
            it.addItemDecoration(
                RecycleViewDivider(
                    requireContext(),
                    LinearLayoutManager.HORIZONTAL,
                    1,
                    ContextCompat.getColor(requireContext(), R.color.gray_c9c9ca)
                )
            )
        }
        mViewModel.onRequestText(bean)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listJsonArray.observe(viewLifecycleOwner, {
            for (i in jsonArray.length() - 1 downTo 0) {
                jsonArray.remove(i)
            }
            var list = it as JSONArray
            for (i in 0 until list.length()) {
                val obj = list.optJSONObject(i)
                jsonArray.put(obj)
            }
            adapter!!.notifyDataSetChanged()
        })

        eventViewModel.uploadAsset.observeInFragment(this, {
            mViewModel.onRequestText(Gson().toJson(it))
        })
    }

}